package com.juc.singtontest;

/**
 * @author yuanzheng
 * @date 2020/8/14-21:27
 */
public class LazyMan2 {
    private volatile static LazyMan2 lazyMan2;

    private LazyMan2() {
        System.out.println(Thread.currentThread().getName() + "-getIt!");
    }

    public static LazyMan2 getInstance() {
        if (lazyMan2 == null) {
            synchronized (LazyMan2.class) {
                if (lazyMan2 == null) {
                    lazyMan2 = new LazyMan2();
                }
            }
        }
        return lazyMan2;
    }

    // 无多线程会出现问题
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                LazyMan2.getInstance();
            }).start();
        }
    }
}
